package regex;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javafx.util.Pair;
import utils.StringUtils;

/**
 * @Description:
 * @author: fengrun@pinduoduo.com
 * @date: 2019-05-12
 */
public class UrlUtils {
    private static final String TAG = "UrlUtils";
    static final Pattern sPattern = SJPatterns.WEB_URL;
    static final Pattern sPatternPhone = Pattern.compile("^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(17[013678])|(18[0,5-9]))\\\\d{8}$");
    static final Pattern sPatternPhone2 = SJPatterns.PHONE;
    private static final Pattern PHONE_NUMBER_PATTERN =
        Pattern.compile("[\\+]?[0-9.-]{11,}");

    private UrlUtils() {}

    public static List<Pair<Integer, Integer>> indexOfUrl(String text) {
        List<Pair<Integer, Integer>> list = new ArrayList<>();
        if (StringUtils.isEmpty(text)) {
            return list;
        }

        Matcher m = sPattern.matcher(text);
        while (m.find()) {
            Pair<Integer, Integer> pair = new Pair<>(m.start(), m.end());
            list.add(pair);
        }

        return list;
    }

    public static List<Pair<Integer, Integer>> indexOfPhone(String text) {
        List<Pair<Integer, Integer>> list = new ArrayList<>();
        if (StringUtils.isEmpty(text)) {
            return list;
        }

        Matcher m = sPatternPhone.matcher(text);
        while (m.find()) {
            Pair<Integer, Integer> pair = new Pair<>(m.start(), m.end());
            list.add(pair);
        }

        return list;
    }

    public static boolean matches(String url) {
        if (StringUtils.isEmpty(url)) {
            return false;
        }

        return sPattern.matcher(url).matches();
    }
}
